package com.uroad.jilinpda.fragment;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.TextView;


import com.uroad.jilinpda.R;
import com.uroad.jilinpda.bleactivation.TopUpUtil;
import com.uroad.jilinpda.modle.OBUVehicleInfoMDL;
import com.uroad.jilinpda.net.JsonUtil;
import com.uroad.jilinpda.webservice.ActivateWS;
import com.uroad.lib.fragment.BaseFragment;

import org.json.JSONException;
import org.json.JSONObject;


/**
 * @创建者 wlw
 * @创建时间 2019/6/16 17:04
 * @描述
 */
public class OBUVehicleInfoFragment extends BaseFragment {
    private View view;
    TextView tvPlateNo;
    TextView tvPlateColor;
    TextView tvCarType;
    TextView tvCarUserType;
    TextView tvCarSize;
    TextView tvCarWidth;
    TextView tvWheelNum;
    TextView tvAxlesNum;
    TextView tvWheelBase;
    TextView tvLoad;
    TextView tvVehicleFeature;
    TextView tvEngineNo;
    private LinearLayout llBusLoad;
    private LinearLayout llBusEngineNo;

    private LinearLayout llTruckQuality;
    private LinearLayout llTruckLoad;
    private LinearLayout llTruckCurbWeight;
    private TextView tvLoadingQuality;
    private TextView tvLoadNum;
    private TextView tvCurbQuality;

    private String fileVehicle = "";
    private String obuNo = "";
    private String randomObu = "";
    private String fileSystem = "";
    private OBUVehicleInfoMDL mdl;
    private boolean isTruck = false;

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        view = inflater.inflate(R.layout.view_obu_vehicle_info, null);
        //        ButterKnife.bind(view);
        initView();
        SetData();
        return view;
    }

    private void initView() {
        tvPlateNo = view.findViewById(R.id.tvPlateNo);
        tvPlateColor = view.findViewById(R.id.tvPlateColor);
        tvCarType = view.findViewById(R.id.tvCarType);
        tvCarUserType = view.findViewById(R.id.tvCarUserType);
        tvCarSize = view.findViewById(R.id.tvCarSize);
        tvCarWidth = view.findViewById(R.id.tvCarWidth);
        tvWheelNum = view.findViewById(R.id.tvWheelNum);
        tvAxlesNum = view.findViewById(R.id.tvAxlesNum);
        tvWheelBase = view.findViewById(R.id.tvWheelBase);
        tvLoad = view.findViewById(R.id.tvLoad);
        tvVehicleFeature = view.findViewById(R.id.tvVehicleFeature);
        tvEngineNo = view.findViewById(R.id.tvEngineNo);

        llBusLoad = view.findViewById(R.id.llBusLoad);
        llBusEngineNo = view.findViewById(R.id.llBusEngineNo);
        llTruckQuality = view.findViewById(R.id.llTruckQuality);
        llTruckLoad = view.findViewById(R.id.llTruckLoad);
        llTruckCurbWeight = view.findViewById(R.id.llTruckCurbWeight);
        tvLoadingQuality = view.findViewById(R.id.tvLoadingQuality);
        tvLoadNum = view.findViewById(R.id.tvLoadNum);
        tvCurbQuality = view.findViewById(R.id.tvCurbQuality);
    }

    private void SetData() {
        loadData(obuNo, fileVehicle);
    }

    private void loadData(String obuNo, String fileVehicle) {
        if (!TextUtils.isEmpty(fileVehicle)&&fileVehicle.length()>118) {
            doRequest(ActivateWS.decodeinstructions,
                    ActivateWS.decodeinstructionsParams(
                            obuNo,fileVehicle.substring(8,fileVehicle.length()-4),fileSystem,randomObu),
                    ActivateWS.decodeinstructions);
        }
    }

    public void setContent(String obuNo, String fileVehicle, String random, String filesystem) {
        if (!TextUtils.isEmpty(fileVehicle)&&fileVehicle.length()>62) {
            if (tvPlateNo != null&&tvPlateColor!=null&&tvCarUserType!=null&&tvCarType!=null
                    &&tvCarSize!=null&&tvWheelNum!=null&&tvLoad!=null) {
                setData(mdl);
            }else{
                this.fileVehicle = fileVehicle;
                this.obuNo = obuNo;
                this.fileSystem = filesystem;
                this.randomObu = random;
            }
        }
    }

    @Override
    public void OnHttpTaskComplete(String result, String type) {
        try {
            JSONObject jsonObject = new JSONObject(result);
            if (JsonUtil.GetJsonStatu00(jsonObject)) {
                String vehicleInfoHex = jsonObject.getString("data");
//                String vehicleInfoHex = data.getString("vehicleInfoHex");
                if (!TextUtils.isEmpty(vehicleInfoHex)) {
                    OBUVehicleInfoMDL mdl = TopUpUtil.getObuVehicleInfo(vehicleInfoHex.toUpperCase());
                    OBUVehicleInfoMDL mdl2 = TopUpUtil.getObuTruckVehicleInfo(vehicleInfoHex.toUpperCase());
                    if (mdl.getCarType().endsWith("货车")) {
                        isTruck = true;
                        this.mdl = mdl2;
                    } else {
                        isTruck = false;
                        this.mdl = mdl;
                    }
                    setData(this.mdl);
                }
            }
        } catch (JSONException e) {
            e.printStackTrace();
        }
    }


    private void setData(OBUVehicleInfoMDL mdl) {
        if (mdl != null) {
            tvPlateNo.setText(mdl.getPlateNo());
            tvPlateColor.setText(mdl.getPlateColor());
            tvCarUserType.setText(mdl.getCarUserType());
            tvCarType.setText(mdl.getCarType());
            String size = mdl.getCarSize();
            if (!TextUtils.isEmpty(size) && size.length() >= 8) {
                int length;
                int width;
                if (isTruck) {
                    length = Integer.parseInt(size.substring(0, 4), 16);
                    width = Integer.parseInt(size.substring(4, 8), 16);
                    tvCarSize.setText(length + "mm");
                    tvCarWidth.setText(width + "mm");
                }else {
                    length = Integer.parseInt(size.substring(0, 4), 16);
                    width = Integer.parseInt(size.substring(4, 6), 16);
                    tvCarSize.setText(length + "dm");
                    tvCarWidth.setText(width + "dm");
                }
            }
            tvWheelNum.setText(mdl.getWheelNum());
            if (isTruck) {
                llBusLoad.setVisibility(View.GONE);
                llBusEngineNo.setVisibility(View.GONE);

                llTruckQuality.setVisibility(View.VISIBLE);
                llTruckLoad.setVisibility(View.VISIBLE);
                llTruckCurbWeight.setVisibility(View.VISIBLE);

                tvLoadingQuality.setText(Integer.parseInt(mdl.getApprovedQuality(), 16) + "kg");
                tvLoadNum.setText(Integer.parseInt(mdl.getLoadNum(),16)+"人");
                tvCurbQuality.setText(Integer.parseInt(mdl.getCurbQuality(), 16) + "kg");

                tvAxlesNum.setText(mdl.getAxlesNum());
                tvWheelBase.setText(mdl.getWheelBase());
                tvVehicleFeature.setText(mdl.getVehicleFeature());
            }else {
                llBusLoad.setVisibility(View.VISIBLE);
                llBusEngineNo.setVisibility(View.VISIBLE);

                llTruckQuality.setVisibility(View.GONE);
                llTruckLoad.setVisibility(View.GONE);
                llTruckCurbWeight.setVisibility(View.GONE);
                tvLoad.setText(Integer.parseInt(mdl.getLoad(), 16) + "kg/座");
                tvAxlesNum.setText(mdl.getAxlesNum());
                tvWheelBase.setText(mdl.getWheelBase());
                tvVehicleFeature.setText(mdl.getVehicleFeature());
                String engineNo = TopUpUtil.asciiToString(mdl.getEngineNo());
                tvEngineNo.setText(engineNo);
            }
        }
    }
}
